
const tokens = {
  admin: {
    token: 'admin-token'
  },
  editor: {
    token: 'editor-token'
  }
}

//1.导入mockjs
const Mock = require('mockjs')


//2.声明返回结果集合
const list = []
const count = 100

//3.扩展mockjs生成手机号
Mock.Random.extend({
  phone: function () {
    var phonePrefixs = ['135', '189', '159', '132']
    return this.pick(phonePrefixs) + Mock.mock(/\d{8}/)
  }
})

//4.生成用户查询 的 结果集合
for (let i = 0; i < count; i++) {
  list.push(Mock.mock({
    id: '@increment',
    createDate: '@date(yyyy-MM-dd)',
    userName: '@first',
    nickName: '@first',
    'sex|1': ['MALE', 'FEMALE'],
    'status|1': ['DISABLE', 'ENABLED'],
    phonenumber: '@phone'
  }))
}


const users = {
  'admin-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Super Admin'
  },
  'editor-token': {
    roles: ['systemAdmin'],
    introduction: 'I am an editor',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Normal Editor'
  }
}

module.exports = [
  // user login
  {
    url: '/vue-element-admin/user/login',
    type: 'post',
    response: config => {
      const { username } = config.body
      const token = tokens[username]

      // mock error
      if (!token) {
        return {
          code: 60204,
          message: 'Account and password are incorrect.'
        }
      }

      return {
        code: 20000,
        data: token
      }
    }
  },

  // get user info
  {
    url: '/vue-element-admin/user/info\.*',
    type: 'get',
    response: config => {
      const { token } = config.query
      const info = users[token]

      // mock error
      if (!info) {
        return {
          code: 50008,
          message: 'Login failed, unable to get user details.'
        }
      }

      return {
        code: 20000,
        data: info
      }
    }
  },

  // user logout
  {
    url: '/vue-element-admin/user/logout',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  },
  // 用户列表分页查询功能mock测试数据
  {
    url: '/vue-element-admin/user',
    type: 'get',
    response: config => {

      //解构赋值  对应listQuery 查询条件+分页条件
      const { userName, status, sex, page = 1, limit = 20,sort } = config.query
      //按照查询条件  -  过滤测试mock数据
      const mockList = list.filter(item => {
        if (userName && item.userName !== userName) return false
        if (status && item.status !== status) return false
        if (sex && item.sex !== sex) return false
        return true
      })
      //排序
      // if (sort === 'DESC') {
      //    mockList = mockList.reverse()
      // }
      //按分页条件进行分页处理
      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      
      //模拟后台返回的数据格式:
      return {
        code: 20000,
        data: {
          total: mockList.length,// 查询后-满足条件的总记录数
          items: pageList        // 查询后-满足条件的当前页的数据集合
        }
      }
    }
  },
  {
    url: '/vue-element-admin/user',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  }, {
    url: '/vue-element-admin/user',
    type: 'delete',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  }

]
